﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using ProjectBase.Mvc.Controllers;

using Survey.Common;
using Survey.Common.Business;
using Survey.Common.Business.Services;
using Survey.Common.Controllers.Services;
using Survey.Common.Models.Services;
using Survey.Entity.Services;

namespace Survey.Mvc.Controllers.Services
{
    /// <summary>
    ///     All actions for Story
    /// </summary>
    public class StoryController
        : ReadController<Article, int, ArticleFilter, IArticleModel, IArticleFacade>, IStoryController
    {
        #region OnList
        /// <summary>
        ///     Provides columns for List view
        /// </summary>
        protected override IEnumerable<IItemToDisplay> LoadItemsToDisplay(ArticleFilter filter)
        {
            filter.AddPropertyToProjectionList(() => Model.Item.ID);
            filter.AddPropertyToProjectionList(() => Model.Item.Code);
            filter.AddPropertyToProjectionList(() => Model.Item.Description);
            filter.AddPropertyToProjectionList(() => Model.Item.Title);
            filter.AddPropertyToProjectionList(() => Model.Item.ReleaseDate);
            filter.AddPropertyToProjectionList(() => Model.Item.LastUpdateDate);
            filter.AddPropertyToProjectionList(() => Model.Item.ArticleCategory.Code,
                () => Model.Item.ArticleCategoryCode);
            filter.AddPropertyToProjectionList(() => Model.Item.ArticleGroup.Code,
                () => Model.Item.ArticleGroupCode);

            return Facade.Find(filter)
                .Select(i => new ItemToDisplay
                {
                    ID = i.IDToDisplay,
                    Description = i.ToDisplay(),
                    Items = new List<ICellDescription>
                    {
                        i.Cell(() => i.Title, new CellDescription {MaxCellLength = 50}),
                        i.Cell(() => i.Description, new CellDescription {MaxCellLength = 700}),
                        i.Cell(() => i.ArticleCategoryCode, () => i.ArticleCategory.Code),
                        i.Cell(() => i.ArticleGroupCode, () => i.ArticleGroup.Code),
                        i.Cell(() => i.LastUpdateDate, new CellDescription {Format = "D"}),
                        i.Cell(() => i.Code),
                    }
                } as IItemToDisplay);
        }

        /// <summary>
        ///     Provides columns for Excel
        /// </summary>
        protected override IEnumerable<IEnumerable<IExCell>> LoadExcelItemsToDisplay(ArticleFilter filter)
        {
            Model.Filter.AddPropertyToProjectionList(() => Model.Item.ID);
            Model.Filter.AddPropertyToProjectionList(() => Model.Item.Code);
            Model.Filter.AddPropertyToProjectionList(() => Model.Item.Description);
            Model.Filter.AddPropertyToProjectionList(() => Model.Item.Title);
            Model.Filter.AddPropertyToProjectionList(() => Model.Item.ReleaseDate);
            Model.Filter.AddPropertyToProjectionList(() => Model.Item.LastUpdateDate);
            Model.Filter.AddPropertyToProjectionList(() => Model.Item.ArticleCategory.Code,
                () => Model.Item.ArticleCategoryCode);
            Model.Filter.AddPropertyToProjectionList(() => Model.Item.ArticleGroup.Code,
                () => Model.Item.ArticleGroupCode);

            return Facade.Find(filter)
                .Select(i => new List<IExCell>
                {
                    i.ExCell(() => i.ID),
                    i.ExCell(() => i.Code),
                    i.ExCell(() => i.Title),
                    i.ExCell(() => i.Description),
                    i.ExCell(() => i.ReleaseDate),
                    i.ExCell(() => i.LastUpdateDate),
                    i.ExCell(() => i.ArticleCategoryCode),
                    i.ExCell(() => i.ArticleGroupCode),
                });
        }
        #endregion OnList

        #region OnBind
        #endregion OnBind

        #region OnBefore
        #endregion OnBefore

        #region Actions
        public override ActionResult Detail(int? id)
        {
            if (id.IsNull())
            {
                return RedirectToAction(Str.Actions.Default);
            }

            if (Model.Item.IsNull()
                || !Model.Item.ID.Equals(id.Value))
            {
                Model.Item = Facade.GetById(id.Value);
            }

            return RedirectToAction(Str.Business.Services.Article, new RouteValueDictionary
            {
                {"category", Model.Item.ArticleCategory.Code},
                {"group", Model.Item.ArticleGroup.Code},
                {Str.Common.Code, Model.Item.Code},
            });
        }
        #endregion Actions

        #region Article
        public virtual ActionResult Article(string category, string group, string code)
        {
            BindPath(category, group);

            if (code.IsNotEmpty())
            {
                Model.Item = Facade.GetByCode(code);
                if (Model.Item.Is()
                    && Model.Item.IsVisible)
                {
                    OnBeforeDetail(Model.Item.ID);

                    return View(Str.Business.Services.Article, Model);
                }
            }
            Model.MasterModel.CurrentAction = Str.Actions.List;
            return List();
        }

        protected virtual void BindPath(string category, string group)
        {
            // category
            Model.Filter.ArticleCategory = null;
            if (category.IsNotEmpty())
            {
                Model.Filter.ArticleCategory = ArticleCategoryFacade.GetByCode(category);
            }

            // group
            Model.Filter.ArticleGroup = null;
            if (group.IsNotEmpty())
            {
                Model.Filter.ArticleGroup = ArticleGroupFacade.GetByCode(group);
            }
        }
        #endregion Article

        #region abstract ClearFilter
        /// <summary>
        ///     Pre-sets filtering filters
        /// </summary>
        protected override ArticleFilter ClearFilter()
        {
            var filter = base.ClearFilter();

            filter.IsVisible = true;
            filter.MaxRowsPerPage = 12;

            filter.OrderBy["ReleaseDate"] = false;

            return filter;
        }
        #endregion ClearFilter

        #region properties
        public override string ControllerName { get { return Str.Controllers.Story; } }

        public virtual IArticleCategoryFacade ArticleCategoryFacade { protected get; set; }
        public virtual IArticleGroupFacade ArticleGroupFacade { protected get; set; }
        #endregion properties
    }
}